﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using DTO;
using DAO;
using BUS;
namespace Cucre
{
    public partial class doanhnghiepcapnhat : System.Web.UI.Page
    {
        private static PROVIDER provider;

        public static PROVIDER Provider
        {
            get { return doanhnghiepcapnhat.provider; }
            set { doanhnghiepcapnhat.provider = value; }
        }

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            string path = Server.MapPath("theme.txt");
            StreamReader SR = new StreamReader(path);
            string nameTheme = SR.ReadLine();
            SR.Close();
            base.OnPreInit(e);
            if (Session["MyTheme"] == null)
            {
                Session.Add("MyTheme", nameTheme);
                Page.Theme = (string)Session["MyTheme"];
            }
            else
            {
                Page.Theme = (string)Session["MyTheme"];
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            provider = (PROVIDER)Session["user_login"];

            if (provider != null)
            {
                if (provider_companyname.Text == null || provider_companyname.Text == "" || provider_companyname.Text == provider.COMPANY_NAME)
                {
                    provider_companyname.Text = provider.COMPANY_NAME;
                }
                if (provider_address.Text == null || provider_address.Text == "" || provider_address.Text == provider.ACCOUNT.ADDRESS)
                {
                    provider_address.Text = provider.ACCOUNT.ADDRESS;
                }
                if (provider_introduce.Text == null || provider_introduce.Text == "" || provider_introduce.Text == provider.INTRODUCE_COMPANY)
                {
                    provider_introduce.Text = provider.INTRODUCE_COMPANY;
                }
                if (provider_phone.Text == null || provider_phone.Text == "" || provider_phone.Text == provider.ACCOUNT.PHONE_NUMBER)
                {
                    provider_phone.Text = provider.ACCOUNT.PHONE_NUMBER;
                }
                provider_email.Text = provider.ACCOUNT.EMAIL;
                provider_login.Text = provider.ACCOUNT.USERNAME;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (provider_companyname.Text.Trim() == null || provider_companyname.Text.Trim() == "")
            {
                ErrorProviderName.Visible = true;
                ErrorProviderName.BorderColor = System.Drawing.Color.Red;
            }
            else
                if (provider_introduce.Text.Trim() == null || provider_introduce.Text.Trim() == "")
                {
                    ErrorProviderName.Visible = false;
                    ErrorProviderName.BorderColor = System.Drawing.Color.Black;
                    ErrorProviderIntroduce.Visible = true;
                    ErrorProviderIntroduce.BorderColor = System.Drawing.Color.Red;
                }
                else
                    if (provider_password.Text.Trim() == null || provider_password.Text.Trim() == "")
                    {
                        ErrorProviderIntroduce.Visible = false;
                        ErrorProviderIntroduce.BorderColor = System.Drawing.Color.Black;
                        ErrorProviderPassword.Visible = true;
                        ErrorProviderPassword.BorderColor = System.Drawing.Color.Red;
                    }
                    else
                        if (provider_confpass.Text.Trim() == null || provider_confpass.Text.Trim() == "" || provider_confpass.Text.Trim() != provider_password.Text.Trim())
                        {
                            ErrorProviderPassword.Visible = false;
                            ErrorProviderPassword.BorderColor = System.Drawing.Color.Black;
                            ErrorProviderConfpass.Visible = true;
                            ErrorProviderConfpass.BorderColor = System.Drawing.Color.Red;
                        }
                        else
                            if (provider_address.Text.Trim() == null || provider_address.Text.Trim() == "")
                            {
                                ErrorProviderConfpass.Visible = false;
                                ErrorProviderConfpass.BorderColor = System.Drawing.Color.Black;
                                ErrorProviderAddress.Visible = true;
                                ErrorProviderAddress.BorderColor = System.Drawing.Color.Red;

                            }
                            else
                                if (provider_phone.Text.Trim() == null || provider_phone.Text.Trim() == "")
                                {
                                    ErrorProviderAddress.Visible = false;
                                    ErrorProviderAddress.BorderColor = System.Drawing.Color.Black;
                                    ErrorProviderPhone.Visible = true;
                                    ErrorProviderPhone.BorderColor = System.Drawing.Color.Red;
                                }
                                else
                                    if (full_name.Text.Trim() == null || provider_password.Text.Trim() == "")
                                    {
                                        ErrorProviderPhone.Visible = false;
                                        ErrorProviderPhone.BorderColor = System.Drawing.Color.Black;
                                        ErrorFullName.Visible = true;
                                        ErrorFullName.BorderColor = System.Drawing.Color.Red;
                                    }
                                    else
                                    {

                                        PROVIDER provider_new = ProviderBUS.GetProvider(provider.IDPROVIDER);
                                        provider_new.COMPANY_NAME = provider_companyname.Text.Trim();
                                        provider_new.ACCOUNT.FULLNAME = full_name.Text.Trim();
                                        provider_new.ACCOUNT.ADDRESS = provider_address.Text.Trim();
                                        provider_new.ACCOUNT.PHONE_NUMBER = provider_phone.Text.Trim();
                                        provider_new.INTRODUCE_COMPANY = provider_introduce.Text.Trim();
                                        bool update = false;
                                        try
                                        {
                                            update = ProviderBUS.Update(provider_new);
                                        }
                                        catch (Exception ex)
                                        {
                                            Response.Write("<script>" + "alert('Bạn đã cập nhật thất bại');" + "</script>");
                                        }
                                        if (update == true)
                                        {
                                            Session.Add("provider_login", provider_new);
                                            Response.Write("<script>" + "alert('Bạn đã cập nhật thành công');window.location.href='index.aspx'" + "</script>");
                                        }
                                        else
                                        {
                                            Response.Write("<script>" + "alert('Cập nhật thất bại');" + "</script>");
                                        }
                                    }
        }
    }
}
